{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "nutritional-belgium",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "proprietary-onion",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 設定從哪顆鏡頭讀取影像，在括弧中填入先前查詢到的webcam編號\n",
    "webcam = cv2.VideoCapture(0)\n",
    "# 讀取影像\n",
    "return_value, image = webcam.read()\n",
    "# 儲存名為Picture.png的照片\n",
    "cv2.imwrite(\"Picture.png\", image)\n",
    "# 刪除webcam，避免影像佔用資源\n",
    "del(webcam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "sufficient-trust",
   "metadata": {},
   "outputs": [],
   "source": [
    "#開啟視窗顯示影像\n",
    "cv2.imshow('Picture.png', image)\n",
    "# 不刷新影像\n",
    "cv2.waitKey(0)\n",
    "# 釋放資源\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "progressive-privacy",
   "metadata": {},
   "source": [
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "electric-college",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from collections import defaultdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "documentary-database",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "executed-stock",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getColorList():\n",
    "    dict = defaultdict(list)\n",
    "    # 红色\n",
    "    lower_red = np.array([156, 43, 46])\n",
    "    upper_red = np.array([180, 255, 255])\n",
    "    color_list = []\n",
    "    color_list.append(lower_red)\n",
    "    color_list.append(upper_red)\n",
    "    dict['red']=color_list\n",
    " \n",
    " \n",
    "    # 红色2\n",
    "    lower_red = np.array([0, 43, 46])\n",
    "    upper_red = np.array([10, 255, 255])\n",
    "    color_list = []\n",
    "    color_list.append(lower_red)\n",
    "    color_list.append(upper_red)\n",
    "    dict['red2'] = color_list\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "professional-trace",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "operating-hawaii",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "go in get_color\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'NoneType' object is not iterable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-51-34d6dca601cf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     30\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     31\u001b[0m     \u001b[0mframe\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mget_color\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-51-34d6dca601cf>\u001b[0m in \u001b[0;36mget_color\u001b[1;34m(frame)\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[0mcolor\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m     \u001b[0mcolor_dict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetColorList\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m     \u001b[1;32mfor\u001b[0m \u001b[0md\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcolor_dict\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m         \u001b[0mmask\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minRange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhsv\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcolor_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcolor_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m         \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;34m'.jpg'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmask\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'NoneType' object is not iterable"
     ]
    }
   ],
   "source": [
    "import  cv2\n",
    "import numpy as np\n",
    "\n",
    " \n",
    "filename='Picture.png'\n",
    " \n",
    "#处理图片\n",
    "def get_color(frame):\n",
    "    print('go in get_color')\n",
    "    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)\n",
    "    maxsum = -100\n",
    "    color = None\n",
    "    color_dict = getColorList()\n",
    "    for d in color_dict:\n",
    "        mask = cv2.inRange(hsv,color_dict[d][0],color_dict[d][1])\n",
    "        cv2.imwrite(d+'.jpg',mask)\n",
    "        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]\n",
    "        binary = cv2.dilate(binary,None,iterations=2)\n",
    "        img, cnts, hiera = cv2.findContours(binary.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n",
    "        sum = 0\n",
    "        for c in cnts:\n",
    "            sum+=cv2.contourArea(c)\n",
    "        if sum > maxsum :\n",
    "            maxsum = sum\n",
    "            color = d\n",
    " \n",
    "    return color\n",
    " \n",
    " \n",
    "if __name__ == '__main__':\n",
    "    frame = cv2.imread(filename)\n",
    "    print(get_color(frame))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exempt-religious",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adapted-grade",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
